package gam.t1.appraisal2.dinv;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import gam.t1.appraisal2.dinv.view.impl.ClickBlock;
import gam.t1.appraisal2.dinv.view.impl.ClickBlockCollections;
import org.bukkit.ChatColor;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public abstract class DataInventory {
    protected String InventoryName;
    protected Inventory View;
    protected final List<ClickBlockCollections> models = Lists.newArrayList();
    protected DataInventory(String inventoryName) {
        this.InventoryName = inventoryName;
    }
    public DataInventory clone(){
        return null;
    }
    public String getInventoryName() {
        return ChatColor.translateAlternateColorCodes('&',InventoryName);
    }
    public boolean hasOccupied(int index){
        for (ClickBlockCollections model : models) {
            for (Object o : model.getStorageStacks(model).keySet()) {
                int i = (int) o;
                if (i == index) return true;
            }
        }
        return false;
    }
    public void add(ClickBlockCollections model) {
        Objects.requireNonNull(model, " Models must not be null");
        if (!models.contains(model)) {
            this.models.add(model);
        }
    }
    public Map<Integer, ItemStack> getItems() {
        Map<Integer, ItemStack> items = Maps.newHashMap();
        for (ClickBlockCollections model : models) {
            items.putAll(model.getStorageStacks(model));
        }
        return items;
    }
    public Inventory getView() {
        return View;
    }
    public void setView(Inventory view) {
        View = view;
    }
    public Map<Integer,ItemStack> runClickBlock(int index){
        for (ClickBlockCollections model : models) {
            ClickBlock var1 = model.getClickBlock(index);
            if (var1 != null) {
                if (var1.getClickHandler().test(model)){
                    return model.getStorageStacks(model);
                } else {
                    return null;
                }
            }
        }
        return null;
    }
}
